<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Object-oriented programming</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="C#, learn C#, OOP, .NET, Mono, object-oriented programming, programming, language">
<meta name="description" content="This part of the C# tutorial covers object-oriented programming.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Object-oriented programming</h1>

<p>
In this part of the C# tutorial, we will talk about object oriented 
programming in C#.
</p>

<p>
There are three widely used programming paradigms there. Procedural 
programming, functional programming and object-oriented programming. 
C# supports both procedural and object-oriented programming.
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script>

<p>
<b>Object-oriented programming (OOP)</b> is a programming paradigm that uses objects
and their interactions to design applications and computer programs. (Wikipedia)
</p>

<p>
There are some basic programming concepts in OOP:
</p>

<ul>
<li>Abstraction</li>
<li>Polymorphism</li>
<li>Encapsulation</li>
<li>Inheritance</li>
</ul>

<p>
The <b>abstraction</b> is simplifying complex reality by modeling classes 
appropriate to the problem. The <b>polymorphism</b> is the process of using 
an operator or function in different ways for different data input. The <b>encapsulation</b> 
hides the implementation details of a class from other objects. The <b>inheritance</b> 
is a way to form new classes  using classes that have already been defined.
</p>

<h2>Objects</h2>

<p>
Objects are basic building blocks of a C# OOP program. 
An object is a combination of data and methods. In a OOP program, 
we create objects. These objects communicate together through methods. 
Each object can receive messages, send messages and process data. 
</p>

<p>
There are two steps in creating an object. First, we define a class. 
A <b>class</b> is a template for an object. It is a blueprint, which 
describes the state and behavior that the objects of the class all share. 
A class can be used to create many objects. Objects created at runtime 
from a class are called <b>instances</b> of that particular class. 
</p>

<pre class="code">
using System;


public class Being {}

public class CSharpApp
{
    static void Main()
    {
        Being b = new Being();
        Console.WriteLine(b);
    }
}
</pre>

<p>
In our first example, we create a simple object. 
</p>

<pre class="explanation">
public class Being {}
</pre>

<p>
This is a simple class definition. The body of the template is empty. 
It does not have any data or methods. 
</p>

<pre class="explanation">
Being b = new Being();
</pre>

<p>
We create a new instance of the Being class. For this we have the 
<code>new</code> keyword. The <code>b</code> variable is the handle to 
the created object. 
</p>

<pre class="explanation">
Console.WriteLine(b);
</pre>

<p>
We print the object to the console to get some basic description 
of the object. What does it mean, to print an object? When we print
an object, we in fact call its <code>ToString()</code> method. But
we have not defined any method yet. It is because every object created
inherits from the base <code>object</code>. It has some elementary
functionality, which is shared among all objects created. One of this
is the <code>ToString()</code> method.
</p>

<pre>
$ ./simpleobject.exe 
Being
</pre>

<p>
We get the object class name.
</p>

<h2>Object attributes</h2>

<p>
Object attributes is the data bundled in an instance of a class. The 
object attributes are called <b>instance variables</b> or <b>member fields</b>.
An instance variable is a variable defined in a class, for which each object
in the class has a separate copy.
</p>

<pre class="code">
using System;


public class Person 
{
    public string name;
}

public class CSharpApp
{
    static void Main()
    {
        Person p1 = new Person();
        p1.name = "Jane";

        Person p2 = new Person();
        p2.name = "Beky";

        Console.WriteLine(p1.name);
        Console.WriteLine(p2.name);
    }
}
</pre>

<p>
In the above C# code, we have a Person class with
one member field. 
</p>


<pre class="explanation">
public class Person 
{
    public string name;
}
</pre>

<p>
We declare a name member field. The <code>public</code>
keyword specifies, that the member field will be accessible
outside the class block.
</p>

<pre class="explanation">
Person p1 = new Person();
p1.name = "Jane";
</pre>

<p>
We create an instance of the Person class. And set the
name variable to "Jane". We use the dot operator to 
access the attributes of objects. 
</p>

<pre class="explanation">
Person p2 = new Person();
p2.name = "Beky";
</pre>

<p>
We create another instance of the Person class. Here
we set the variable to "Beky".
</p>

<pre class="explanation">
Console.WriteLine(p1.name);
Console.WriteLine(p2.name);
</pre>

<p>
We print the contents of the variables to the console. 
</p>

<pre>
$ ./person.exe 
Jane
Beky
</pre>

<p>
We see the output of the program. Each instance of the Person class has a separate
copy of the name member field. 
</p>

<h2>Methods</h2>

<p>
Methods are functions defined inside the body of a class. They are used to perform operations 
with the attributes of our objects. Methods are essential in <b>encapsulation</b> concept of 
the OOP paradigm. For example, we might have a Connect() method in our AccessDatabase class. 
We need not to be informed, how exactly the method Connect() connects to the database. 
We only have to know, that it is used to connect to a database. This is essential in dividing
responsibilities in programming. Especially in large applications. 
</p>

<pre class="code">
using System;


public class Circle 
{
    private int radius; 

    public void SetRadius(int radius)
    {
        this.radius = radius;
    }

    public double Area()
    {
        return this.radius * this.radius * Math.PI;
    }
}

public class CSharpApp
{
    static void Main()
    {
        Circle c = new Circle();
        c.SetRadius(5);

        Console.WriteLine(c.Area());
    }
}
</pre>

<p>
In the code example, we have a Circle class. We define two methods.
</p>

<pre class="explanation">
private int radius;
</pre>

<p>
We have one member field. It is the radius of the circle. The 
<code>private</code> keyword is an access specifier. It tells 
that the variable is restricted to the outside world. If we
want to modify this variable from the outside, we must use
the publicly available <code>SetRadius()</code>  method. This way we protect
our data. 
</p>

<pre class="explanation">
public void SetRadius(int radius)
{
    this.radius = radius;
}
</pre>

<p>
This is the <code>SetRadius()</code> method. The <code>this</code> 
variable is a special variable, which we use to access the member 
fields from methods. The this.radius is the instance variable, the
radius is the local variable, valid only inside the <code>SetRadius()</code> 
method.
</p>

<pre class="explanation">
Circle c = new Circle();
c.SetRadius(5);
</pre>

<p>
We create an instance of the Circle class. And set its radius by 
calling the <code>SetRadius()</code> method on the object of the
circle. We use the dot operator to call the method. 
</p>

<pre class="explanation">
public double Area()
{
    return this.radius * this.radius * Math.PI;
}
</pre>

<p>
The <code>Area()</code> method returns the area of a circle. The 
<code>Math.PI</code> is a built-in constant. 
</p>

<pre>
$ ./circle.exe 
78.5398163397448
</pre>

<p>
Running the example. 
</p>

<h2>Access modifiers</h2>

<p>
<b>Access modifiers</b> set the visibility of methods and member fields. 
C# has four access modifiers. <code>public</code>, <code>protected</code>, 
<code>private</code> and <code>internal</code>. <code>public</code> members can
be accessed from anywhere. <code>protected</code> members can be accessed 
only within the class itself and by inherited and parent classes. <code>private</code>
members are limited to the containing type, e.g only within its class or interface.
<code>internal</code> members may be accessed from within the same assembly (exe or dll). 
</p>

<p>
Access modifiers protect data against accidental modifications. They make 
the programs more robust.
</p>

<pre class="code">
using System;


public class Person 
{
    public string name;
    private int age;

    public int GetAge()
    {
        return this.age;
    }

    public void SetAge(int age)
    {
        this.age = age;
    }
}

public class CSharpApp
{
    static void Main()
    {
        Person p = new Person();
        p.name = "Jane";
 
        p.SetAge(17);

        Console.WriteLine("{0} is {1} years old", 
            p.name, p.GetAge());
    }
}
</pre>

<p>
In the above program, we have two member fields. One is declared public, 
the other private. 
</p>

<pre class="explanation">
public int GetAge()
{
    return this.age;
}
</pre>

<p>
If a member field is <code>private</code>, the only way to access it is via
methods. If we want to modify an attribute outside the class, the
method must be declared <code>public</code>. This is an important aspect of 
data protection. 
</p>

<pre class="explanation">
public void SetAge(int age)
{
    this.age = age;
}
</pre>

<p>
The <code>SetAge()</code> method enables us to change the <code>private</code>
age variable from outside of the class definition.
</p>

<pre class="explanation">
Person p = new Person();
p.name = "Jane";
</pre>

<p>
We create a new instance of the <code>Person</code> class. Because the 
name attribute is <code>public</code>, we can access it directly. 
However, this is not recommended. 
</p>

<pre class="explanation">
p.SetAge(17);
</pre>

<p>
The <code>SetAge()</code> method modifies the age member field. It cannot 
be accessed or modified directly, because it is declared <code>private</code>. 
</p>

<pre class="explanation">
Console.WriteLine("{0} is {1} years old", 
    p.name, p.GetAge());
</pre>

<p>
Finally, we access both members to build a string.
</p>

<pre>
$ ./modifiers.exe 
Jane is 17 years old
</pre>

<p>
Running the example. 
</p>

<hr class="btm">

<pre class="code">
using System;


public class Base 
{
    public string name = "Base";
    protected int id = 5323;
    private bool isDefined = true;
}

public class Derived : Base 
{
    public void info()
    {
        Console.WriteLine("This is Derived class");
        Console.WriteLine("Members inherited");
        Console.WriteLine(this.name);
        Console.WriteLine(this.id);
        // Console.WriteLine(this.isDefined);
    }
}

public class CSharpApp
{
    static void Main()
    {
        Derived drv = new Derived();
        drv.info();
    }
}
</pre>

<p>
In the preceding program, we have a <code>Derived</code> class, which 
inherits from the <code>Base</code> class. The <code>Base</code> class 
has three member fields. All with different access modifiers. The isDefined
member is not inherited. The <code>private</code> modifier prevents this.
</p>

<pre class="explanation">
public class Derived : Base 
</pre>

<p>
The class <code>Derived</code> inherits from the <code>Base</code> class. 
To inherit from another class, we use the colon (:) operator.
</p>

<pre class="explanation">
Console.WriteLine(this.name);
Console.WriteLine(this.id);
// Console.WriteLine(this.isDefined);
</pre>

<p>
The <code>public</code> and the <code>protected</code> members are 
inherited by the Derived class. They can be accessed. The 
<code>private</code> member is not inherited. The line accessing 
the member field is commented. If we uncommented the line, the code 
would not compile.
</p>


<pre>
$ ./protected.exe 
This is Derived class
Members inherited
Base
5323
</pre>

<p>
Running the program, we receive this output.
</p>


<h2>The constructor</h2>

<p>
A constructor is a special kind of a method. It is automatically called, 
when the object is created. Constructors do not return values.
The purpose of the constructor is to initiate the state of an object. 
Constructors have the same name as the class. The constructors
are methods, so they can be overloaded too. Constructors cannot return 
any value. 
</p>

<p>
<b>Constructors</b> Constructors cannot be inherited. They are called in the order of inheritance.
If we don't write any constructor for a class, C# provides an implicit default 
constructor.  If we provide any kind of a constructor than a default is not supplied.
</p>

<pre class="code">
using System;


public class Being 
{
    public Being() 
    {
        Console.WriteLine("Being is created");
    } 

    public Being(string being)
    {
        Console.WriteLine("Being {0} is created", being);

    }
}

public class CSharpApp
{
    static void Main()
    {
        new Being();
        new Being("Tom");
    }
}
</pre>

<p>
We have a Being class. This class has two constructors.
The first one does not take parameters, the second one
takes one parameter. 
</p>

<pre class="explanation">
public Being(string being)
{
    Console.WriteLine("Being {0} is created", being);
}
</pre>

<p>
This constructor takes one string parameter.
</p>

<pre class="explanation">
new Being();
</pre>

<p>
An instance of the Being class is created. This 
time the constructor without a parameter is called upon
object creation. 
</p>

<pre>
$ ./constructor.exe 
Being is created
Being Tom is created
</pre>

<p>
This is the output of the program. 
</p>

<hr class="btm">

<p>
<b>Member initiation</b> In the next example, we initiate data members 
of the class. Initiation of variables is a typical job for constructors. 
</p>


<pre class="code">
using System;


public class MyFriend 
{
    private DateTime born;
    private string name;
   
    public MyFriend(string name, DateTime born)
    {
        this.name = name;
        this.born = born;
    }

    public void Info() 
    {
        Console.WriteLine("{0} was born on {1}",
            this.name, this.born.ToShortDateString());
    } 
}

public class CSharpApp
{
    static void Main()
    {
        string name = "Lenka";
        DateTime born = new DateTime(1990, 3, 5);

        MyFriend fr = new MyFriend(name, born);
        fr.Info();
    }
}
</pre>

<p>
We have a MyFriend class with data members and methods. 
</p>

<pre class="explanation">
private DateTime born;
private string name;
</pre>

<p>
We have two private variables in the class definition. 
</p>

<pre class="explanation">
public MyFriend(string name, DateTime born)
{
    this.name = name;
    this.born = born;
}
</pre>

<p>
In the constructor, we initiate the two data members. The <code>this</code> 
variable is a handler used to reference the object variables. 
</p>

<pre class="explanation">
MyFriend fr = new MyFriend(name, born);
fr.Info();
</pre>

<p>
We create a MyFriend object with two arguments. Then we call the <code>Info()</code> 
method of the object. 
</p>

<pre>
$ ./constructor2.exe 
Lenka was born on 3/5/1990
</pre>

<hr class="btm">

<p>
<b>Constructor chaining</b> Constructor chaining is the ability of a class 
to call another constructor from another constructor. To call another constructor
from the same class, we use the <code>this</code> keyword. 
</p>


<pre class="code">
using System;


public class Circle 
{
    public Circle(int radius)
    {
        Console.WriteLine("Circle, r={0} is created", radius);
    }

    public Circle() : this(1) 
    {

    }
}

public class CSharpApp
{
    static void Main()
    {
        new Circle(5);
        new Circle();
    }
}
</pre>

<p>
We have a Circle class. The class has two constructors. One that takes
one parameter and one that does not take any parameters.
</p>

<pre class="explanation">
public Circle(int radius)
{
    Console.WriteLine("Circle, r={0} is created", radius);
}
</pre>

<p>
This constructor takes one parameter, the radius. 
</p>

<pre class="explanation">
public Circle() : this(1) 
{

}
</pre>

<p>
This is the constructor without a parameter. It simply calls the
other constructor and gives it a default radius of 1. 
</p>

<pre>
$ ./constructorchaining.exe 
Circle, r=5 is created
Circle, r=1 is created
</pre>

<p>
Output of the example. 
</p>

<h2>Class constants</h2>

<p>
C# enables to create class constants. These constants do not belong 
to a concrete object. They belong to the class. By convention, constants are 
written in uppercase letters. 
</p>

<pre class="code">
using System;


public class Math 
{
    public const double PI = 3.14159265359;
}

public class CSharpApp
{
    static void Main()
    {
        Console.WriteLine(Math.PI);
    }
}
</pre>

<p>
We have a Math class with a PI constant. 
</p>

<pre class="explanation">
public const double PI = 3.14159265359;
</pre>

<p>
The <code>const</code> keyword is used to define a constant.
</p>

<pre>
$ ./classconstant.exe 
3.14159265359
</pre>

<p>
Running the example.
</p>


<h2>The ToString() method</h2>

<p>
Each object has a <code>ToString()</code> method. It returns a 
human-readable representation of the object. The default implementation 
returns the fully qualified name of the type of the Object. Note that when 
we call the <code>Console.WriteLine()</code> method with an 
object as a parameter, the <code>ToString()</code> is being called.
</p>

<pre class="code">
using System;


public class Being 
{
    public override string ToString()
    {
        return "This is Being class";
    }
}

public class CSharpApp
{
    static void Main()
    {
        Being b = new Being();
        object o = new Object();
        
        Console.WriteLine(o.ToString());
        Console.WriteLine(b.ToString());
        Console.WriteLine(b);
    }
}
</pre>

<p>
We have a Being class in which we override the default implementation
of the <code>ToString()</code> method. 
</p>

<pre class="explanation">
public override string ToString()
{
    return "This is Being class";
}
</pre>

<p>
Each class created inherits from the base <code>object</code>. 
The <code>ToString()</code> method belongs to this object class.
We use the <code>override</code> keyword to inform, that we are overriding 
a method.
</p>

<pre class="explanation">
Being b = new Being();
object o = new Object();
</pre>

<p>
We create two objects. One custom defined and one built-in. 
</p>

<pre class="explanation">
Console.WriteLine(o.ToString());
Console.WriteLine(b.ToString()); 
</pre>

<p>
We call the <code>ToString()</code> method on these two objects.
</p>

<pre class="explanation">
Console.WriteLine(b);
</pre>

<p>
As we have specified earlier, calling the <code>Console.WriteLine()</code>
on the object will call its <code>ToString()</code> method.
</p>

<pre>
$ ./override.exe 
System.Object
This is Being Class
This is Being Class
</pre>

<p>
This is what we get, when we run the example. 
</p>


<h2>Inheritance</h2>

<p>
The inheritance is a way to form new classes using classes 
that have already been defined. The newly formed classes are 
called <b>derived</b> classes, the classes that we derive from
are called <b>base</b> classes. Important benefits of inheritance 
are code reuse and reduction of complexity of a program. The 
derived classes (descendants) override or extend the functionality 
of base classes (ancestors). 
</p>

<pre class="code">
using System;


public class Being 
{
    public Being()
    {
        Console.WriteLine("Being is created");
    }
}

public class Human : Being
{
    public Human()
    {
        Console.WriteLine("Human is created");
    }
}

public class CSharpApp
{
    static void Main()
    {
        new Human();
    }
}
</pre>

<p>
In this program, we have two classes. A base Being class and a 
derived  Human class. The derived class inherits from the 
base class. 
</p>

<pre class="explanation">
public class Human : Being
</pre>

<p> 
In C#, we use the colon (:) operator to create inheritance 
relations.
</p>

<pre class="explanation">
new Human();
</pre>

<p>
We instantiate the derived Human class. 
</p>

<pre>
$ ./inheritance.exe 
Being is created
Human is created
</pre>

<p>
We can see, that both constructors were called. First, the 
constructor of the base class is called, then the constructor 
of the derived class.
</p>

<hr class="btm">

<p>
A more complex example follows. 
</p>

<pre class="code">
using System;


public class Being 
{
    static int count = 0;

    public Being()
    { 
        count++;
        Console.WriteLine("Being is created");
    }

    public void GetCount()
    {
        Console.WriteLine("There are {0} Beings", count);
    }
}

public class Human : Being
{
    public Human()
    {
        Console.WriteLine("Human is created");
    }
}

public class Animal : Being
{
    public Animal()
    {
        Console.WriteLine("Animal is created");
    }
}

public class Dog : Animal
{
    public Dog()
    {
        Console.WriteLine("Dog is created");
    }
}

public class CSharpApp
{
    static void Main()
    {
        new Human();
        Dog dog = new Dog();
        dog.GetCount();
    }
}
</pre>

<p>
We have four classes. The inheritance hierarchy is more complicated. The
Human and the Animal classes inherit from the Being class. And the Dog class inherits directly
from the Animal class and indirectly from the Being class. We also introduce a 
concept of a <code>static</code> variable.
</p>

<pre class="explanation">
static int count = 0;
</pre>

<p>
We define a <code>static</code> variable. Static members
are members, that are shared by all instances of a class. 
</p>

<pre class="explanation">
public Being()
{ 
    count++;
    Console.WriteLine("Being is created");
}
</pre>

<p>
Each time the Being class is instantiated, we increase the count
variable by one. This way we keep track of the number of instances 
created.
</p>

<pre class="explanation">
public class Animal : Being
...

public class Dog : Animal
...
</pre>

<p>
The Animal inherits from the Being and the Dog inherits from the Animal. 
Indirectly, the Dog inherits from the Being as well. 
</p>

<pre class="explanation">
new Human();
Dog dog = new Dog();
dog.GetCount();
</pre>

<p>
We create instances from the Human and from the Dog classes.
We call the <code>GetCount()</code> method of the Dog object. 
</p>

<pre>
$ ./inheritance2.exe 
Being is created
Human is created
Being is created
Animal is created
Dog is created
There are 2 Beings
</pre>

<p>
The Human object calls two constructors. The Dog object 
calls three constructors. There are two Beings instantiated.
</p>

<hr class="btm">

<p>
<b>Calling the parent's constructor</b> We use the <code>base</code>
keyword to call the parent's constructor explicitly. 
</p>

<pre class="code">
using System;


public class Shape 
{
    protected int x;
    protected int y;

    public Shape() 
    {
        Console.WriteLine("Shape is created");
    } 

    public Shape(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

public class Circle : Shape
{
    private int r;    

    public Circle(int r, int x, int y) : base(x, y)
    {
        this.r = r;
    }

    public override string ToString()
    {
        return String.Format("Circle, r:{0}, x:{1}, y:{2}", r, x, y);
    }
}

public class CSharpApp
{
    static void Main()
    {
        Circle c = new Circle(2, 5, 6);
        Console.WriteLine(c);
    }
}
</pre>

<p>
We have two classes. The Shape class and the Circle class. The Shape
class is a base class for geometrical shapes. We can put into this class
some commonalities of the common shapes. Like the x, y coordinates. 
</p>

<pre class="explanation">
public Shape() 
{
    Console.WriteLine("Shape is created");
} 
public Shape(int x, int y)
{
    this.x = x;
    this.y = y;
}
</pre>

<p>
The Shape class has two constructors. The first one is the default constructor.
The second one takes two parameters, the x, y coordinates. 
</p>

<pre class="explanation">
public Circle(int r, int x, int y) : base(x, y)
{
    this.r = r;
}
</pre>

<p>
This is the constructor for the Circle class. This constructor initiates the
r member and calls the parent's second constructor, to which it passes the x, y coordinates.
Have we not called the constructor explicitly with the <code>base</code> keyword, the
default constructor of the Shape class would be called. 
</p>

<pre>
$ ./shapes.exe 
Circle, r:2, x:5, y:6
</pre>

<p>
Output of the example. 
</p>


<h2>Abstract classes and methods</h2>

<p>
Abstract classes cannot be instantiated. If a class contains at 
least one abstract method, it must be declared abstract too. 
Abstract methods cannot be implemented, they merely declare the methods' 
signatures. When we inherit from an abstract class, all abstract methods
must be implemented by the derived class. Furthermore, these methods must 
be declared with the same of less restricted visibility. 
</p>

<p>
Unlike <b>Interfaces</b>, abstract classes may have methods with full implementation and may also have
defined member fields. So abstract classes may provide a partial implementation. Programmers often put
some common functionality into abstract classes. And these abstract classes are later subclassed to 
provide more specific implementation. For example, the Qt graphics library has a QAbstractButton, which 
is the abstract base class of button widgets, providing functionality common to buttons. Buttons 
Q3Button, QCheckBox, QPushButton, QRadioButton, and QToolButton inherit from this base abstract class. 
</p>

<p>
Formally put, abstract classes are used to enforce a protocol. 
A protocol is a set of operations, which all implementing objects 
must support. 
</p>

<pre class="code">
using System;


public abstract class Drawing
{

    protected int x = 0;
    protected int y = 0;

    public abstract double Area();

    public string GetCoordinates()
    {
        return string.Format("x: {0}, y: {1}", this.x, this.y);
    }
}


public class Circle : Drawing
{
    private int r; 

    public Circle(int x, int y, int r)
    {
        this.x = x;
        this.y = y;
        this.r = r;
    }

    public override double Area()
    {
        return this.r * this.r * Math.PI;
    }

    public override string ToString()
    {
        return string.Format("Circle at x: {0}, y: {1}, radius: {2}", 
            this.x, this.y, this.r);
    }
}

public class CSharpApp
{
    static void Main()
    {
        Circle c = new Circle(12, 45, 22);
        Console.WriteLine(c);
        Console.WriteLine("Area of circle: {0}", c.Area());
        Console.WriteLine(c.GetCoordinates());
    }
}
</pre>

<p>
We have an abstract base Drawing class. The class defines two member fields, 
defines one method and declares one method. One of the methods is abstract, 
the other one is fully implemented. The Drawing class is abstract, because 
we cannot draw it. We can draw a circle, a dot or a square. The Drawing class 
has some common functionality to the objects, that we can draw. 
</p>

<pre class="explanation">
public abstract class Drawing
</pre>

<p>
We use the <code>abstract</code> keyword to define an abstract class. 
</p>

<pre class="explanation">
public abstract double Area();
</pre>

<p>
An abstract method is also preceded with a <code>abstract</code> keyword.
</p>

<pre class="explanation">
public class Circle : Drawing
</pre>

<p>
A Circle is a subclass of the Drawing class. It must implement the
abstract <code>Area()</code> method. 
</p>

<pre class="explanation">
public override double Area()
{
    return this.r * this.r * Math.PI;
}
</pre>

<p>
When we implement the <code>Area()</code> method, we must use the 
<code>override</code> keyword. This way we inform the compiler, that
we override an existing (inherited) method.
</p>


<pre>
$ ./abstractclass.exe 
Circle at x: 12, y: 45, radius: 22
Area of circle: 1520.53084433746
x: 12, y: 45
</pre>

<p>
Output of the program. 
</p>

<p>
This was the first part of the description of OOP in C#.
</p>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified November 7, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body> 
</html>

